// dao/userDao.js
// 实现与MySQL交互
var mysql = require('mysql');
var $conf = require('../conf/db');
var $util = null; //require('../util/util');
var $sql = require('./liveTextSqlMapping');
var logger = require('../util/log').logger;

// 使用连接池，提升性能
var pool = mysql.createPool($conf.mysql);

// 向前台返回JSON方法的简单封装
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code : '1',
            msg : '操作失败'
        });
    } else {
        res.json(ret);
    }
};

module.exports = {
    insert : function (param) {
        pool.getConnection(function (err, connection) {
            if (err) {
                logger.error("liveTextDao-insert:DB Connection Error: " + err.message);
                return;
            }
            var useDbSql = "USE ummor";
            connection.query(useDbSql, function (err) {
                if (err) {
                    console.log("liveTextDao-insert:USE Error: " + err.message);
                    return;
                }
            });

            // 建立连接，向表中插入值
            // 'INSERT INTO user(id, name, age) VALUES(0,?,?)',
			logger.debug('liveTextData-insert:sql:'+$sql.insert+';params:match_msg_id='+param.match_msg_id+';game_time='+param.game_time+';current_stage='+param.current_stage+';'+'team_id='+param.team_id+';user_id='+param.user_id+';user_name='+param.user_name+';current_score='+param.current_score+';text='+param.text+';action_id='+param.action_id+';time='+param.time+';team_name='+param.team_name);
            connection.query($sql.insert, [param.match_msg_id, param.game_time, param.current_stage, param.team_id, param.user_id, param.user_name, param.current_score, param.text, param.action_id,param.time,param.team_name], function (err, result) {
                //console.log("query:"+err+":"+result[0].account);
                if (result) {
                    result = {
                        code : 200,
                        msg : '增加成功'
                    };
					logger.info('liveTextDao-insert:insert success!');
                }
                if (err) {
                    logger.error('liveTextDao-insert  error:'+err.message);
                }

                // 释放连接
                connection.release();
            });
        });
    },

    /**
     *根据赛事信息ID查询赛事信息
     *@param: 赛事ID
    @callback: function(err,result)
     */
    select : function (param, callback) {
        pool.getConnection(function (err, connection) {

            var useDbSql = "USE ummor";
            connection.query(useDbSql, function (err) {
                if (err) {
                    logger.error("liveTextDao-select:USE Error: " + err.message);
                    return;
                }

                logger.info('Execute sql:' + $sql.select+';param='+param);
                connection.query($sql.select, [param], function (err, rows, fields) {
                    if (err) {
                        logger.error("liveTextDao-select:Execute sql Error: " + err.message);
                    }
					
                    if (rows) {
                        logger.info('liveTextDao-select:records:' + rows.length + ',fields:' + fields.length);
                    }

                    if (callback) {
                        callback(err, {
                            rows : rows
                        });
                    }
                    // 释放连接
                    connection.release();
                    logger.info('connection release finish!');

                });
            });

        });
    },
    
    /**
     *根据赛事信息ID获取最新的比分排行
     *@param: 赛事ID
     *@callback: function(err,result)
     */
    select_user_range : function (param, callback) {
        logger.debug("select-getConnection,param:" + param);
        pool.getConnection(function (err, connection) {

            var useDbSql = "USE ummor";
            connection.query(useDbSql, function (err) {
                if (err) {
                    logger.error("liveTextDao-select_user_range:USE Error: " + err.message);
                    return;
                }

                logger.info('Execute sql:' + $sql.select+';match_msg_id='+param);
                connection.query($sql.select_user_range, [param], function (err, rows, fields) {
                    if (err) {
                        logger.error("liveTextDao-select_user_range:Execute sql Error: " + err.message);
                    }

                    // 以json形式，把操作结果返回给前台页面
                    //jsonWrite(res, result);
                    if (rows) {
                        logger.info('records:' + rows.length + ',fields:' + fields.length);
                    }

                    if (callback) {
                        callback(err, {
                            rows : rows
                        });
                    }
                    // 释放连接
                    connection.release();
                    logger.info('connection release finish!');

                });
            });

        });
    }

    };
